import type { MenuItem } from '@/apis'
import { userApis } from '@/apis'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  const userInfo = ref<IUserInfo>({} as IUserInfo)

  const menuList = ref<MenuItem[]>([])

  const setUserInfo = (info: IUserInfo) => {
    userInfo.value = info
  }

  const getMenuList = async () => {
    return new Promise(async (resolve, reject) => {
      try {
        const res = await userApis.getMenuList()
        if (res.status === 0) {
          menuList.value = res.data
          resolve(res.data)
        }
        else {
          reject(res.msg)
        }
      }
      catch (error) {
        console.error(error)
        reject(error)
      }
    })
  }

  return { userInfo, setUserInfo, menuList, getMenuList }
}, {
  persist: {
    storage: sessionStorage,
  },
})
